import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/LoginView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/host',
      name: 'Host',
      component: () => import('../views/HostView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/terminal',
      name: 'Terminal',
      component: () => import('../views/TerminalView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/database/:type',
      name: 'Database',
      component: () => import('../views/Database.vue'),
      meta: { requiresAuth: true }
    }
  ],
})

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})


export default router
